package com.googlecode.scenemaker3d.view.timeline
{
	import com.googlecode.scenemaker3d.events.HideMarkerMoveEvent;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import com.googlecode.scenemaker3d.model.VO_Object3D;
	
	import mx.core.Application;
	import mx.core.BitmapAsset;
	import mx.core.UIComponent;

	public class SplitMarker extends UIComponent
	{
		private var VO:VO_Object3D;
		private var hideMarkerID:String;
		
		private var intervalWidth:uint;
		
		private var lMarker:Sprite = new Sprite();
		private var rMarker:Sprite = new Sprite();
		private var mWidth:Number;
		
		private var markerLine:Sprite = new Sprite();
		private var markerLineGraphic:Sprite = new Sprite();
		private var mlWidth:uint;
		private var mlHeight:uint;
		
		private var hArea:Sprite;
		private var dragging:Sprite;
		private var draggingOffset:Point;
		private var prevPos:Object = new Object();
		
		private var markerY:int;
		
		
		[Embed(source="com/googlecode/scenemaker3d/view/timeline/assets/splitMarker.gif")]
		[Bindable]
		private var splitMarkerImg:Class;
		
		[Event(name="hideMarker", type="events.HideMarkerMoveEvent")]
		public function SplitMarker(w:Number, h:Number, time:Number, y:int)
		{
			super();
			
			intervalWidth = w;
			markerY = y;
			//Make split marker
			mWidth = Math.round(intervalWidth /3);
			
			lMarker.name = "left";
			rMarker.name = "right";
			
			var lIcon:BitmapAsset = new splitMarkerImg();
			var rIcon:BitmapAsset = new splitMarkerImg();
			
			lIcon.width = mWidth;
			rIcon.width = mWidth;
			
			lIcon.height = h;
			rIcon.height = h;
			
			rIcon.x = -mWidth;
				
			lMarker.addChild(lIcon);
			rMarker.addChild(rIcon);	
			
			lMarker.x = Math.round(time  /intervalWidth)* intervalWidth;
			lMarker.y = markerY;
			rMarker.x = lMarker.x + intervalWidth;
			rMarker.y = markerY;
			
			hArea = getHitArea();
			
			Application.application.addEventListener(MouseEvent.MOUSE_UP, onStageUp);
			Application.application.addEventListener(MouseEvent.MOUSE_MOVE, onStageMove);
			Application.application.addEventListener(MouseEvent.ROLL_OUT, onStageOut);
			
			lMarker.addEventListener(MouseEvent.MOUSE_DOWN, onMarkerDown);			
			rMarker.addEventListener(MouseEvent.MOUSE_DOWN, onMarkerDown);
			
			mlWidth = intervalWidth;
			mlHeight = Math.round(h/3) *2;
			
			markerLineGraphic.graphics.beginFill(0xFF0000);
			markerLineGraphic.graphics.drawRect(0, 0, mlWidth, mlHeight);
			markerLineGraphic.y = Math.round(mlHeight /3);
			
			markerLine.addChild(markerLineGraphic);
			markerLine.x = lMarker.x;
			markerLine.y = markerY;
			
			markerLine.addEventListener(MouseEvent.MOUSE_DOWN, onMarkerDown);
			
			this.addChild(markerLine);		
			this.addChild(lMarker);
			this.addChild(rMarker);
			
		}
		private function onStageUp(e:MouseEvent):void
		{
			if(!dragging)
			{
				return;
			}
			dragging.removeChild(hArea);
			dragging = null;
			prevPos = {start:lMarker.x, end:rMarker.x - intervalWidth};
			
			this.dispatchEvent(new HideMarkerMoveEvent(HideMarkerMoveEvent.HIDE_MARKER_MOVE, {start:lMarker.x, end:rMarker.x - intervalWidth}));
		}
		private function onStageOut(e:MouseEvent):void
		{
			if(!dragging)
			{
				return;
			}
			dragging.removeChild(hArea);
			dragging = null;
			prevPos = {start:lMarker.x, end:rMarker.x - intervalWidth};
			
			this.dispatchEvent(new HideMarkerMoveEvent(HideMarkerMoveEvent.HIDE_MARKER_MOVE, {start:lMarker.x, end:rMarker.x - intervalWidth}));
		}
		private function onStageMove(e:MouseEvent):void
		{
			if(!dragging)
			{
				return;
			}
			
			dragging.y = markerY;
			
			prevPos = {start:lMarker.x, end:rMarker.x - intervalWidth};	

			var stepPos:Number = Math.round((this.mouseX - draggingOffset.x) /intervalWidth)* intervalWidth;
			dragging.x = stepPos;
			
			if(dragging.x < 0)
			{
				dragging.x = 0;	
			}
			
			if(dragging == markerLine)
			{
				lMarker.x = markerLine.x;
				rMarker.x = markerLine.x + mlWidth;
				
				return;
			}			
			//Step
						
			if(dragging.name == "left")
			{
				if(dragging.x > rMarker.x - intervalWidth)
				{
					dragging.x = rMarker.x - intervalWidth;
				}
			}else{
				if(dragging.x < lMarker.x + intervalWidth)
				{
					dragging.x = lMarker.x + intervalWidth;					
				}
			} 
			
			mlWidth = rMarker.x - lMarker.x;
			markerLine.x = lMarker.x;
			markerLineGraphic.graphics.clear();
			markerLineGraphic.graphics.beginFill(0xFF0000);
			markerLineGraphic.graphics.drawRect(0, 0, mlWidth, mlHeight);
			
			//Only dispatch an event if the position has changed
			if(prevPos.start != lMarker.x || prevPos.end != rMarker.x - intervalWidth)
			{
				this.dispatchEvent(new HideMarkerMoveEvent(HideMarkerMoveEvent.HIDE_MARKER_MOVE, {start:lMarker.x, end:rMarker.x - intervalWidth}));
			}
			
		}
		private function onMarkerDown(e:MouseEvent):void
		{
			draggingOffset = new Point(e.localX, e.localY);
			
			(e.currentTarget as Sprite).addChild(hArea);
				
			dragging = (e.currentTarget as Sprite);	
		}
		private function getHitArea():Sprite
		{
			var hArea:Sprite = new Sprite();
			hArea.name = "hArea";
			
			hArea.graphics.beginFill(0x000000, 0);
			hArea.graphics.drawRect(-200, -200, 400, 400);
			
			return hArea;
		}
		override public function get id():String
		{
			return this.hideMarkerID;
		}
		public function get marker():Object{
			var m:Object = new Object();
			m.start = lMarker.x;
			m.end = rMarker.x - intervalWidth;
			return m;
		}
		public function set marker(m:Object):void{
			
		}
		public function set object3D(VO:VO_Object3D):void
		{
			this.VO = VO;
		}
		public function get object3D():VO_Object3D
		{
			return VO;
		}
	}
}